﻿using System.ComponentModel.DataAnnotations;
using BootstrapAdmin.DataAccess.Models;

namespace BootstrapAdmin.Web.ViewModels.Production;

public sealed class AlarmRecordSearchModel : ITableSearchModel
{
    /// <summary>
    /// 警报分类
    /// </summary>
    [Display(Name = "警报分类")]
    public string? Classify { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    [Display(Name = "记录时间")]
    [NotNull]
    public DateTimeRangeValue? CreatedTime { get; set; }

    public AlarmRecordSearchModel()
    {
        Reset();
    }

    public IEnumerable<IFilterAction> GetSearchs()
    {
        var ret = new List<IFilterAction>();

        if (!string.IsNullOrEmpty(Classify))
        {
            ret.Add(new SearchFilterAction(nameof(AlarmRecord.Classify), Classify));
        }

        if (CreatedTime != null)
        {
            ret.Add(new SearchFilterAction(nameof(AlarmRecord.CreatedAt), CreatedTime.Start, FilterAction.GreaterThanOrEqual));
            ret.Add(new SearchFilterAction(nameof(AlarmRecord.CreatedAt), CreatedTime.End, FilterAction.LessThanOrEqual));
        }

        return ret;
    }

    public void Reset()
    {
        Classify = null;
        CreatedTime = new DateTimeRangeValue
        {
            Start = DateTime.Now.AddDays(-7),
            End = DateTime.Now
        };
    }
}
